<!DOCTYPE html>
<html>
#{include file="header.html"}#
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox-content">
        <form class="form-horizontal" method="post" action="#">
            <div class="form-group text-left">
                <label class="col-sm-2 control-label">供应商名称</label>
                <div class="col-sm-8">
                    <select data-placeholder="选择分类..." name="supplier_id" class="chosen-select supplier-chosen-select"
                            style="width: 200px;" tabindex="2">
                        <option value="">请选供应商</option>
                        #{foreach from=$supplier key=k item=v}#
                        <option value="#{$v.supplier_id}#" hassubinfo="true">#{$v.name}#</option>
                        #{/foreach}#

                    </select>
                    <input type="hidden" name="supplier_name">
                </div>
            </div>
            <div class="form-group text-left">
                <label class="col-sm-2 control-label">采购订单</label>
                <div class="col-sm-8">
                    <select data-placeholder="请选供应商采购订单..." name="contract_id" class="chosen-select contract-chosen-select" style="width: 200px;" tabindex="1">
                        <option value="">请选供应商采购订单</option>
                    </select>
                    <input type="hidden" name="contract_name">
                </div>
            </div>
            <div class="form-group">
          <label class="col-sm-2 control-label">总金额</label>
          <div class="col-sm-8">
            <input name="contract_money" class="form-control" type="text" readonly/>
            <span class="help-block m-b-none"></span></div>
        </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">去零金额</label>
            <div class="col-sm-8">
              <input name="contract_zero_money" class="form-control" type="text" readonly/>
              <span class="help-block m-b-none"></span></div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">已付金额</label>
            <div class="col-sm-8">
              <input name="contract_pay_money" class="form-control" type="text" readonly/>
              <input type="hidden" name="contract_owe_money">
            </div>

          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">已收发票金额</label>
            <div class="col-sm-8">
              <input name="contract_invoice_money" class="form-control" type="text" readonly/>
              <span class="help-block m-b-none"></span></div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">付款日期</label>
            <div class="col-sm-10">
              <input name="pay_date" class="form-control datepicker" type="text" placeholder="选择计划付款日期"/>
              <span class="help-block m-b-none"></span></div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">期次</label>
            <div class="col-sm-8">
              <input name="stages" class="form-control" type="text" placeholder="请输入付款期次"/>
              <span class="help-block m-b-none"></span></div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">付款金额</label>
            <div class="col-sm-8">
              <input name="pay_money" class="form-control calculate" type="text"/>
              <span class="help-block m-b-none"></span></div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">去零金额</label>
            <div class="col-sm-8">
              <input name="zero_money" class="form-control calculate" type="text" value="0"/>
              <span class="help-block m-b-none"></span></div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">尚欠金额</label>
            <div class="col-sm-8">
              <input name="owe_money" class="form-control" type="text" readonly/>
              <span class="help-block m-b-none"></span></div>
          </div>
            <div class="form-group text-left">
                <label class="col-sm-2 control-label">付款账户</label>
                <div class="col-sm-8">
                    <select data-placeholder="选择付款账户..." name="bank_id" class="chosen-select bank-chosen-select"
                            style="width: 200px;" tabindex="2">
                        <option value="">请选付款账户</option>
                        #{foreach from=$bank key=k item=v}#
                        <option value="#{$v.bank_id}#" hassubinfo="true">#{$v.name}# #{$v.card}#</option>
                        #{/foreach}#
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-8">
                    <button class="btn btn-w-m btn-info save-form" type="button">保存数据</button>
                </div>
            </div>
        </form>
    </div>
</div>
<!-- 自定义js -->
<script src="#{$smarty.const.APP}#/View/template/js/content.js?v=1.0.0"></script>
<script>
    $(document).ready(function () {
        $(".datepicker").datepicker({
            language: "zh-CN",
            autoclose: true,//选中之后自动隐藏日期选择框
            clearBtn: true,//清除按钮
            todayBtn: false,//今日按钮
            format: "yyyy-mm-dd",
        });

        $('.chosen-select').chosen({search_contains: true});
        $(".chosen-select").val("#{$supplier_id}#").trigger("chosen:updated");

        var chance_url = "#{$smarty.const.ACT}#/erp/PosContract/pos_contract_select_pay/";
        //选择用户跳出联系人
        $('.supplier-chosen-select').on('change', function (e, params) {
            change_val = $(this).val();
            change_text = $(this).find('option:selected').text();
            $(".form-horizontal input[name='supplier_name']").val(change_text);
            findPosContractChosenSelect('contract-chosen-select', chance_url, change_val);
            contract_id = $('.contract-chosen-select option:selected').val();
            calc_contract(contract_id);
        });

        //选择采购单跳出关联订单
        $('.contract-chosen-select').on('change', function(e, params) {
            change_val=$(this).val();
            contract_id=$('.contract-chosen-select option:selected').val();
            calc_contract(contract_id);
        });

        //调用关联订单数据
        function calc_contract(contract_id) {
            $.ajax({
                type: "POST",
                url: "#{$smarty.const.ACT}#/erp/PosContract/pos_contract_get_one_json/",
                data: {"contract_id": contract_id},
                dataType: "json",
                success: function (data) {
                    $(".form-horizontal input[name='contract_money']").val(data.money);
                    $(".form-horizontal input[name='contract_zero_money']").val(data.zero_money);
                    $(".form-horizontal input[name='contract_pay_money']").val(data.pay_money);
                    $(".form-horizontal input[name='contract_owe_money']").val(data.owe_money);
                    $(".form-horizontal input[name='contract_invoice_money']").val(data.invoice_money);
                    $(".form-horizontal input[name='pay_money']").val(data.owe_money);
                    $(".form-horizontal input[name='contract_name']").val(data.title);
                },
                complete: function () {
                },
            });
        }

        $("body").on("click", ".save-form", function () {
            FormData = $("form").serialize();
            $.ajax({
                type: "POST",
                url: "#{$smarty.const.ACT}#/erp/FinPayRecord/fin_pay_record_add/",
                data: FormData,
                dataType: "json",
                success: function (data) {
                    if (data.statusCode == '200') {
                        layer.msg('操作成功', {icon: 1});
                    } else {
                        layer.msg(data.message, {icon: 5});
                    }
                },
                complete: function () {
                    setTimeout(function () {
                        //关闭窗口
                        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);
                    }, 800);

                },
            });
        });
        $("body").on("keyup", ".calculate", function () {
            //查询本行的数据
            var contract_money = $(".form-horizontal input[name='contract_money']").val();
            var contract_pay_money = $(".form-horizontal input[name='contract_pay_money']").val();
            var contract_owe_money = $(".form-horizontal input[name='contract_owe_money']").val();
            var contract_zero_money = $(".form-horizontal input[name='contract_zero_money']").val();
            var contract_invoice_money = $(".form-horizontal input[name='contract_invoice_money']").val();
            var pay_money = $(".form-horizontal input[name='pay_money']").val();
            var zero_money = $(".form-horizontal input[name='zero_money']").val();

            var owe_money = parseFloat(contract_owe_money) - parseFloat(pay_money) - parseFloat(zero_money);
            if (owe_money < 0) {
                layer.msg('本次付款的金额和去零金额不能超过 ' + contract_owe_money, {icon: 5});
            }
            $(".form-horizontal input[name='owe_money']").val(owe_money);
            console.log(owe_money);
        });
    });
</script>
</body>
</html>